<template>
  <div class="mod-menu">
    <div class="mb10">
      <el-button @click="getBack">返回秒杀列表</el-button>
    </div>
    <el-form :inline="true" :model="pamars">
      <el-form-item label="商品名称">
        <el-input v-model="pamars.title" placeholder="商品名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="pamars.page=1;getDataList()">查询</el-button>
        <el-button @click="addOrUpdateHandle()">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" row-key="Id" border style="width: 100%; ">
      <el-table-column prop="title" align="center" label="商品名称">
      </el-table-column>
      <el-table-column prop="min_sale_price" align="center" label="销售价格">
      </el-table-column>
      <el-table-column prop="min_price" align="center" label="活动价格">
      </el-table-column>
      <el-table-column prop="limit_buy_num" align="center" label="限购">
        <template slot-scope="scope">
          <span v-if="scope.row.limit_buy_num == 0">不限购</span>
          <span v-else>{{scope.row.limit_buy_num}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="real_stock_total" align="center" label="可售数量">
      </el-table-column>
      <el-table-column prop="result_sale_num" align="center" label="已售数量">
      </el-table-column>
      <el-table-column prop="consumption_value" align="center" label="贡献值">
      </el-table-column>
      <el-table-column prop="sort" align="center" label="商品排序">
      </el-table-column>
      <el-table-column prop="recommended" align="center" label="是推荐商品" :formatter="recommendedFormatter">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="updateHandle(scope.row.id,scope.row.goods_id)">编辑</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pamars.page" :page-sizes="[10, 20, 50, 100]" :page-size="pamars.limit" :total="totalPage" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    <!-- 弹窗, 新增 -->
    <seckill-pd-add-update v-if="addOrUpdateVisible" ref="seckillPdAddUpdate"></seckill-pd-add-update>
    <!-- 弹窗,  修改 -->
    <activity-pd-add v-if="activityPdAdd" ref="activityPdAdd" @updataList='updataList'></activity-pd-add>
  </div>
</template>

<script>
import seckillPdAddUpdate from './seckill-pd-add-update'
import activityPdAdd from './activity-pd-add'
import seviceApi from '@/api/serviceApi.js'
export default {
  data () {
    return {
      dataForm: {},
      dataList: [],
      pamars: {
        page: 1,
        limit: 10,
        title: '',
        seckill_id: ''
      },
      totalPage: 0,
      dataListLoading: false,
      addOrUpdateVisible: false, // 添加
      activityPdAdd: false // 修改
    }
  },
  components: {
    seckillPdAddUpdate,
    activityPdAdd
  },
  mounted () {
    this.pamars.seckill_id = this.$route.query.id
    this.getList()
  },
  watch: {
    '$route' () {
      this.getList()
    }
  },
  methods: {
    recommendedFormatter(row, column) {
      if (row.recommended === true) {
        return '是'
      } else  {
        return '否'
      }
    },
    updataList () {
      debugger
      this.getList()
    },
    // 获取列表
    getList () {
      seviceApi.getSeckillActivityGoodsList(this.pamars).then(res => {
        if (res && res.data.code == 200) {
          this.dataList = res.data.page.list
          this.totalPage = res.data.page.totalCount
          console.log(res.data.page.list)
        }
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pamars.limit = val
      this.getList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pamars.page = val
      this.getList()
    },
    // 查询
    getDataList () {
      this.getList()
    },
    // 新增
    addOrUpdateHandle () {
      this.$router.push({
        path: '/seckill-pd-add-update',
        query: {
          seckill_id: this.pamars.seckill_id
        }
      })
    },
    // 修改
    updateHandle (id, goodsId) {
      this.activityPdAdd = true
      this.$nextTick(() => {
        this.$refs.activityPdAdd.init(id, this.pamars.id, goodsId)
      })
    },
    // 删除
    deleteHandle (id) {
      this.$confirm(`确定要删除该条数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        seviceApi.delSeckillActivityGoods(id).then(res => {
          if (res && res.data.code == 200) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.getList()
          }
        })
      }).catch(() => { })
    },
    // 返回秒杀列表
    getBack () {
      this.$router.go(-1)
    }
  }
}
</script>
<style>
.mb10 {
  margin-bottom: 10px;
}
</style>
